package com.ruoyi.develop.orderIronRecordBuy.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.orderIronRecord.domain.OrderIronRecord;
import com.ruoyi.develop.orderIronRecord.service.IOrderIronRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.orderIronRecordBuy.domain.OrderIronRecordBuy;
import com.ruoyi.develop.orderIronRecordBuy.service.IOrderIronRecordBuyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 铁件拆解记录与采购单关联Controller
 *
 * @author lmm
 * @date 2025-06-21
 */
@RestController
@RequestMapping("/orderIronRecordBuy/orderIronRecordBuy")
public class OrderIronRecordBuyController extends BaseController {
    @Autowired
    private IOrderIronRecordBuyService orderIronRecordBuyService;

    @Autowired
    private IOrderIronRecordService orderIronRecordService;

    /**
     * 查询铁件拆解记录与采购单关联列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderIronRecordBuy entity) {
        startPage();
        List<OrderIronRecordBuy> list = orderIronRecordBuyService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出铁件拆解记录与采购单关联列表
     */
    @Log(title = "铁件拆解记录与采购单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderIronRecordBuy entity) {
        List<OrderIronRecordBuy> list = orderIronRecordBuyService.selectList(entity);
        ExcelUtil<OrderIronRecordBuy> util = new ExcelUtil<OrderIronRecordBuy>(OrderIronRecordBuy.class);
        util.exportExcel(response, list, "铁件拆解记录与采购单关联数据");
    }

    /**
     * 获取铁件拆解记录与采购单关联详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(orderIronRecordBuyService.selectById(id));
    }

    /**
     * 新增铁件拆解记录与采购单关联
     */
    @Log(title = "铁件拆解记录与采购单关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody OrderIronRecordBuy entity) {
        return toAjax(orderIronRecordBuyService.insert(entity));
    }

    /**
     * 批量新增铁件拆解记录与采购单关联
     */
    @Log(title = "铁件拆解记录与采购单关联", businessType = BusinessType.INSERT)
    @PostMapping("/insertOrderRecordRel")
    public AjaxResult insertOrderRecordRel(@RequestBody OrderIronRecordBuy entity) {
        List<OrderIronRecord> orderIronRecordList = entity.getOrderIronRecordList();
        int insertCount = 0;
        for (OrderIronRecord orderIronRecord : orderIronRecordList) {
            OrderIronRecordBuy orderIronRecordBuy = new OrderIronRecordBuy();
            orderIronRecordBuy.setIronRecordId(orderIronRecord.getId());
            orderIronRecordBuy.setOrderId(entity.getOrderId());
            orderIronRecordBuy.setMoney(orderIronRecord.getBuyMoneyTemp());
            orderIronRecordBuy.setPayRemain(orderIronRecord.getBuyMoneyTemp());
            orderIronRecordBuy.setOrderCode(orderIronRecord.getOrderCode());
            orderIronRecordBuy.setOrderId(orderIronRecord.getOrderId());
            orderIronRecordBuy.setStatus(BusinessConstants.STATUS_NO);
            int i = orderIronRecordBuyService.insert(orderIronRecordBuy);
            orderIronRecord.setBuyRemain(orderIronRecord.getBuyRemain().subtract(orderIronRecord.getBuyMoneyTemp()));
            orderIronRecord.setBuyMoney(orderIronRecord.getBuyMoney().add(orderIronRecord.getBuyMoneyTemp()));
            orderIronRecordService.update(orderIronRecord);
            insertCount += i;
        }
        return toAjax(insertCount);
    }


    /**
     * 修改铁件拆解记录与采购单关联
     */
    @Log(title = "铁件拆解记录与采购单关联", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody OrderIronRecordBuy entity) {
        return toAjax(orderIronRecordBuyService.update(entity));
    }

    /**
     * 删除铁件拆解记录与采购单关联
     */
    @Log(title = "铁件拆解记录与采购单关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional
    public AjaxResult remove(@PathVariable String[] ids) {
        for (String id : ids) {
            OrderIronRecordBuy orderIronRecordBuy = orderIronRecordBuyService.selectById(id);
            OrderIronRecord orderIronRecord = orderIronRecordService.selectById(orderIronRecordBuy.getIronRecordId());
            orderIronRecord.setBuyRemain(orderIronRecord.getBuyRemain().add(orderIronRecordBuy.getMoney()));
            orderIronRecord.setBuyMoney(orderIronRecord.getBuyMoney().subtract(orderIronRecordBuy.getMoney()));
            orderIronRecordService.update(orderIronRecord);
        }
        return toAjax(orderIronRecordBuyService.deleteByIds(ids));
    }
}
